<?php
	/**
	 * Created by Shauan Andrade.
	 * User: shauan
	 * Date Time: 19/05/13 | 13:12
	 * Project: yonkou
	 */
	App::uses('AppHelper','View/Helper');
	class FunctionHelper extends AppHelper {

		public function Situacao($value = null)
		{
			$class = '';
			if(!empty($value))
			{
				switch($value)
				{
					case 1:
						$class = 'green';
						break;
					case 2:
						$class = 'blue';
						break;
					case 3:
						$class = 'magenta';
						break;
					case 4:
						$class = 'red';
						break;
				}
			}

			return $class;
		}

		public function dateDBFormat($dateFormat=null){
			$dia= null;
			$mes = null;
			$ano = null;
			$data = "00/00/0000";
			if(!empty($dateFormat)){
				list($ano,$mes,$dia) = explode('-',$dateFormat);
				$data = $dia."/".$mes."/".$ano;
				return $data;
			}
			return $data;
		}

		public function DateFormat($value = array())
		{
			$date = '';
			$return_date = '';
			if(!empty($value) && is_array($value))
			{
				list($date,$time) = explode(' ',$value['date']);
				$separator = isset($value['separator']);
				$separator = empty($separator)? '-' : $separator;
				list($year,$months,$day) = explode($separator,$date);
				$day = $day <= 31 ? $day : 00;
				$months = $months <= 12 ? $months : 00;
				$year = strlen($year) == 4 || strlen($year) == 2  ? $year : 0000;

				$months_ext = array(array('0'=>'','01'=>'Jan','02'=>'Fev','03'=>'Mar','04'=>'Abr','05'=>'Mai','06'=>'Jun','07'=>'Jul','08'=>'Ago','09'=>'Set','10'=>'Out','11'=>'Nov','12'=>'Dez'),array('0'=>'','01'=>'Janeiro','02'=>'Fevereiro','03'=>'Mar&ccedil;o','04'=>'Abril','05'=>'Maio','06'=>'Junho','07'=>'Julho','08'=>'Agosto','09'=>'Setembro','10'=>'Outubro','11'=>'Novembro','12'=>'Dezembro'));
				$this->Session->setFlash('teste');
				$week_ext = array(array('1'=>'Seg','2'=>'Ter','3'=>'Qua','4'=>'Qui','5'=>'Sex','6'=>'Sab','0'=>'Dom'),array('1'=>'Segunda','2'=>'Ter&ccedil;a','3'=>'Quarta','4'=>'Quinta','5'=>'Sexta','6'=>'Sábado','0'=>'Domingo'));
				$weekDay = date("w", mktime(0,0,0,$months,$day,$year) );
				if($value['type'] == 1)
				{
					$return_date = $months_ext[1][$months].' '.$day.', '.$year;
				}
				elseif($value['type']=='DW')
				{
					$return_date = $week_ext[1][$weekDay];
				}
				elseif($value['type']== 2)
				{
					$return_date = $day.'/'.$months.'/'.$year;
				}
			}
			return $return_date;
		}

		public function Status($valor)
		{
			$_status = '';
			if(empty($valor))
			{
				$_status = '0';
			}
			switch($valor)
			{
				case 0:
					$_status = '<span class="label text-center label-danger">Desativado</span>';
					break;
				case 1:
					$_status = '<span class="label text-center label-success">Ativo</span>';
					break;
				default:
					$_status = '<span class="label text-center label-default">ERROR STATUS</span>';
					break;
			}
			return $_status;
		}

		public function Active($active)
		{
			if(isset($active))
			{
                $active = 'class="'.$active.'"';
				return $active;
			}
			return null;
		}

		public function limiteString($text,$num)
		{
			if(strlen($text)<=$num)
			{
				return  $text;
			}
			else
			{
				$y=substr($text,0,$num) . '...';
				return $y;
			}
		}


		/**
		 * showMessage method
		 * Formata o metodo setFlash de modo mais facil
		 * @var $message String;
		 * @var $other Array('type','close','title');
		 */
		public function showMessage($message,$option =array())
		{
			$msg = '';
			if(!empty($message)){
				$btnCloseMsg = '<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>';

				$msg = '<div class="alert alert-default">'.$btnCloseMsg.$message.'</div>';

				if((is_array($option)) && (count($option) > 0)){
					$title	= isset($option['title']) && !empty($option['title'])? "<strong>".$option['title']."</strong>&nbsp;":'';
					$type	= isset($option['type']) && !empty($option['type'])? $option['type']: 'warning';
					$class	= isset($option['class']) && !empty($option['class'])? $option['class']: '';
					$close	= isset($option['close']) && $option['close']==false? '':$btnCloseMsg;
					$message = $close.$title.$message;
					$msg = '<div class="alert alert-'.$type.' '.$class.'">'.$message.'</div>';
				}
			}
			return $msg;
		}

	}
